package com.launch.carmanager.auth;

import com.blankj.utilcode.util.LogUtils;
import com.launch.carmanager.common.base.BasePresenter;
import com.launch.carmanager.common.base.BaseResponse;
import com.launch.carmanager.common.base.BaseView;
import com.launch.carmanager.common.rx.ApiSubscriber;
import com.launch.carmanager.data.ApiInvoker;
import com.launch.carmanager.data.model.UserInfo;

import java.util.Map;

/**
 * Created by ganquan on 2019/3/28 15:03
 */
public class AuthPresenter extends BasePresenter<AuthContract.View> implements AuthContract.Presenter {


    public AuthPresenter(AuthContract.View mView) {
        super(mView);
    }

    @Override
    public void sendCode(String mobilePhone) {
        addSubscription(ApiInvoker.sendCode(mobilePhone).subscribe(new ApiSubscriber<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (baseResponse.getErrcode() == 0) {
                    mView.sendCodeSuccess();
                    LogUtils.e("发送验证码成功 ");
                } else {
                    alert(baseResponse.getMsg());
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                alert(msg);
                LogUtils.e("发送验证码失败 ; code : " + code + " ; msg : " + msg);
            }
        }));
    }


    @Override
    public void auth(String phone, String isCheckCode, String registerStatus) {
        addSubscription(ApiInvoker.login(phone, isCheckCode, registerStatus).subscribe(new ApiSubscriber<UserInfo>() {
            @Override
            public void onSuccess(UserInfo userInfo) {
                mView.authSuccess();
                LogUtils.e("登录授权成功 " + userInfo.toString());
            }

            @Override
            public void onFailure(int code, String msg) {
                alert(msg);
                LogUtils.e("登录授权失败 ; code : " + code + " ; msg : " + msg);
            }
        }));

    }

    @Override
    public void checkLoginUser(String phone) {

    }
}
